#!/usr/bin/perl

use Test;
BEGIN { plan tests => 23 }

$basedir = $0;
$basedir =~ s|(.*)/[^/]*|$1|;

# ================================================================= #
#  Basic Semaphore Test Cases                                       #
# ================================================================= #
# First time should just get the resource
$output = `runcon -t test_ipc_base_t $basedir/semget`;
ok( $?, 0 );
$output =~ /id = (\d+)$/ || print "Invalid output from semget\n";
$ipcid = $1;

# Test ipc_info permission
$output = `runcon -t test_ipc_base_t -- $basedir/getinfo`;
ok( $?, 0 );

$output = `runcon -t test_ipc_none_t -- $basedir/getinfo`;
ok($?);

# Delete the resource
$output = `runcon -t test_ipc_base_t ipcrm sem $ipcid`;
ok( $?, 0 );

# Create it again
$output = `runcon -t test_ipc_base_t $basedir/semget`;
ok( $?, 0 );
$output =~ /id = (\d+)$/ || print "Invalid output from semget\n";
$ipcid = $1;

# Create it one more time to check associate permission
$output = `runcon -t test_ipc_base_t $basedir/semget`;
ok( $?, 0 );
$output =~ /id = (\d+)$/ || print "Invalid output from semget\n";
$ipcid2 = $1;

# Make sure they match
ok( $ipcid2, $ipcid );

# Try to associate with it from the read-only domain
$output = `runcon -t test_ipc_read_t $basedir/semget`;
ok( $?, 0 );
$output =~ /id = (\d+)$/ || print "Invalid output from semget\n";
$ipcid = $1;

# Try to associate with it from the unprivileged domain
$output = `runcon -t test_ipc_none_t $basedir/semget`;
ok($?);
$output =~ /id = (\d+)$/ || print "Invalid output from semget\n";
$ipcid = $1;

# ================================================================= #
# Semaphore Control Tests (semctl syscall)                          #
# ================================================================= #

# run semaphore control tests, we expect all to fail
$output = `runcon -t test_ipc_associate_t $basedir/semctl`;
&reformat_results( $output, -1 );

# run semaphore control tests, we expect all to succeed
# last test should delete the semaphore
$output = `runcon -t test_ipc_base_t $basedir/semctl`;
&reformat_results( $output, 0 );

# ================================================================= #
# Semaphore Operation Tests (semop)                                 #
# ================================================================= #

$output = `runcon -t test_ipc_base_t $basedir/semop`;
ok( $?, 0 );

$output = `runcon -t test_ipc_associate_t $basedir/semop`;
ok($?);    # -1

# Cleanup
$output = `runcon -t test_ipc_base_t $basedir/semget`;
$output =~ /id = (\d+)$/ || print "Invalid output from semget\n";
$ipcid  = $1;
$output = `runcon -t test_ipc_base_t ipcrm sem $ipcid`;

# ================================================================= #
# Miscellaneous Routines                                            #
# ================================================================= #
sub reformat_results {
    $list     = shift;
    $expected = shift;
    for ( split( ' ', $list ) ) {
        ok( $_, $expected );
    }
}
